package com.example.xm.ui.home.fragments;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.xm.R;
import com.example.xm.adapter.HomeAdapter.HomeArticleAdapter;
import com.example.xm.base.BaseFragment;
import com.example.xm.bean.HomeBean.HomeArticleBean;
import com.example.xm.bean.HomeBean.HomeBannderBean;
import com.example.xm.bean.HomeBean.HomeHotUserBean;
import com.example.xm.bean.HomeBean.HomePhotoBean;
import com.example.xm.bean.HomeBean.HomeRecommendBean;
import com.example.xm.bean.HomeBean.HomeRewardBean;
import com.example.xm.bean.HomeBean.HomeSquareBean;
import com.example.xm.bean.HomeBean.HomeTopicDiscussedBean;
import com.example.xm.bean.HomeBean.HomeVideoBean;
import com.example.xm.interfaces.home.IHome;
import com.example.xm.presenter.home.RecommendPresenter;
import com.example.xm.ui.home.fragments.activity_article.Article_DaShiJianActivity;
import com.example.xm.ui.home.fragments.activity_article.Article_RenShiActivity;
import com.example.xm.ui.home.fragments.activity_article.Article_YanJinActivity;
import com.example.xm.ui.home.fragments.activity_article.Article_chuantongActivity;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.config.BannerConfig;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class Fragment_Article extends BaseFragment<IHome.RecommendPersenter> implements IHome.RecommendView {
    @BindView(R.id.tv_ttile_article)
    TextView tvTtileArticle;
    @BindView(R.id.banner_article)
    Banner bannerArticle;
    @BindView(R.id.ll_renshi)
    LinearLayout llRenshi;
    @BindView(R.id.ll_chuantong)
    LinearLayout llChuantong;
    @BindView(R.id.ll_yanjin)
    LinearLayout llYanjin;
    @BindView(R.id.ll_dashijian)
    LinearLayout llDashijian;
    @BindView(R.id.v)
    View v;
    @BindView(R.id.tv_wenzhang)
    TextView tvWenzhang;
    @BindView(R.id.v_wenzhang)
    View vWenzhang;
    @BindView(R.id.rlv_article)
    RecyclerView rlvArticle;
    private ArrayList<HomeArticleBean.DataBean.ArticlesBean> list;
    private HomeArticleAdapter homeArticleAdapter;


    @Override
    protected IHome.RecommendPersenter initPresenter() {
        return new RecommendPresenter();
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_article;
    }

    @Override
    protected void initView() {
        list = new ArrayList<>();
        rlvArticle.setLayoutManager(new LinearLayoutManager(getActivity()));
        rlvArticle.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayout.VERTICAL));
        homeArticleAdapter = new HomeArticleAdapter(getActivity(), list);
        rlvArticle.setAdapter(homeArticleAdapter);
    }

    @Override
    protected void initData() {
        mPresenter.getHomeArticle();
    }

    @Override
    protected void initListener() {
        //认识汉服点击监听
        llRenshi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), Article_RenShiActivity.class);
                startActivity(intent);
            }
        });
        //传统礼仪点击监听
        llChuantong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), Article_chuantongActivity.class);
                startActivity(intent);
            }
        });
        //汉服演进史点击监听
        llYanjin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), Article_YanJinActivity.class);
                startActivity(intent);
            }
        });
        //汉服大事件点击监听
        llDashijian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), Article_DaShiJianActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void showTips(String tips) {

    }

    @Override
    public void showLoading(int visble) {

    }

    @Override
    public void getBannerReturn(HomeBannderBean result) {

    }

    @Override
    public void getTopicDiscussedReturn(HomeTopicDiscussedBean result) {

    }

    @Override
    public void getRecommendReturn(HomeRecommendBean result) {

    }

    @Override
    public void getHotUserReturn(HomeHotUserBean result) {

    }

    @Override
    public void getSquareReturn(HomeSquareBean result) {

    }

    @Override
    public void getVideoReturn(HomeVideoBean result) {

    }

    @Override
    public void getPhotoReturn(HomePhotoBean result) {

    }

    @SuppressLint("WrongConstant")
    @Override
    public void getHomeArticleReturn(HomeArticleBean result) {
        if (result.getStatus().getStatusCode() == 100) {
            List<HomeArticleBean.DataBean.WeekArticlesBean> weekArticles = result.getData().getWeekArticles();
            ArrayList<String> images = new ArrayList<>();
            ArrayList<String> titles = new ArrayList<>();
            for (int i = 0; i < weekArticles.size(); i++) {
                String cover = weekArticles.get(i).getCover();
                String contentDescribe = weekArticles.get(i).getContentDescribe();
                images.add(cover);
                titles.add(contentDescribe);
            }
            bannerArticle.setAdapter(new BannerImageAdapter<String>(images) {
                @Override
                public void onBindView(BannerImageHolder holder, String data, int position, int size) {
                    //图片加载自己实现
                    Glide.with(holder.itemView)
                            .load(data)
                            .apply(RequestOptions.bitmapTransform(new RoundedCorners(80)))//圆角弧度
                            .into(holder.imageView);
                }
            })
                    .addBannerLifecycleObserver(this)//添加生命周期观察者
                    .setIndicatorGravity(BannerConfig.LOOP_TIME)//设置指示器的位置
                    .setIndicator(new CircleIndicator(getActivity()));
            list.addAll(result.getData().getArticles());
            homeArticleAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getHomeRewardReturn(HomeRewardBean result) {

    }
}
